package com.lsa.iview.chap1;

public class StringQuestions {
	public static boolean isRotation(String src, String rtd) {
		if (src.length() != rtd.length()) {
			return false;
		}
		
		int j = 0;
		for (; j < rtd.length(); j++) {
			if (rtd.charAt(j) == src.charAt(0)) {
				break;
			}
		}
		
		if (j == rtd.length()) {
			return false;
		}
		
		int i = 1;
		for (; i < rtd.length() - j; i++) {
			if (src.charAt(i) != rtd.charAt(j + i)) {
				break;
			}
		}
		
		if (i != rtd.length() - j) {
			return false;
		}
		
		return rtd.startsWith(""+src.subSequence(i, src.length() - 1)); 
	}
	
	public static boolean isAllCharsUniqueInplace(String s) {
		for (int i = 0; i < s.length(); i++) {
			for (int j = i + 1; j < s.length(); j++) {
				if (s.charAt(i) == s.charAt(j)) {
					return false;
				}
			}
		}
		return true;
	}
	
	public static boolean isAllCharsUniqueAscii(String s) {
		boolean[] isAlreadyEncountered = new boolean[256];
		for (int i = 0; i < s.length(); i++) {
			char idx = s.charAt(i);
			if (isAlreadyEncountered[idx]) {
				return false;
			}
			isAlreadyEncountered[idx] = true;
		}
		return true;
	}	
}
